<?php

class Application_Model_DbTable_Administrator extends Zend_Db_Table_Abstract
{

    protected $_name = 'administrator';

    /**
     * nome, cognome, username, mail dell'admin con id=$id
     */
    public function getAdmin($id)
        {
        $id = (int)$id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
            throw new Exception("Could not find row $id");
        }
        return $row->toArray();
        }

    /**
     * questa funzione ritorna un array contenente
     * tutti i locali associati all'admin con id=$id
     *
     * @param <int> $id
     * @return <array>
     *
     */
    public function getClub($id){
        $piva = $this->getPiva($id);
        foreach ($piva as $piva2){
           $disco[] = $this->getDiscoBypiva($piva2);
        }
        return $disco;
    }

    /*
     * seleziono solo la piva (o le piva) associate
     * ad admin con id=$id.
     */
    public function getPiva($id){

        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT piva FROM administration WHERE id='" .$id. "'";
            $piva=$db->fetchAll($statement);
            //$piva=$db->fetchRow($statement);
            if (!$piva) {
            throw new Exception("Could not find row $id");
              }
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $piva;
    }

    /*
     * Ritorno tutte le info su di un locale
     * in base alla sua piva.
     */
    public function getDiscoBypiva($piva){

        $piva=$piva['piva'];
        
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT * FROM disco WHERE piva='" .$piva. "'";
            $disco=$db->fetchRow($statement);
            if (!$disco) {
            throw new Exception("Could not find row $piva");
              }
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $disco;
    }

     /*
     * Ritorno tutte le info su di un locale
     * in base alla suo nome.
     */
    public function getDiscoByName($name){
        
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT * FROM disco WHERE name='" .$name. "'";
            $disco=$db->fetchRow($statement);
            if (!$disco) {
            throw new Exception("Could not find row $name");
              }
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $disco;
    }

    /*
     * Elenco di tutti i nomi dei club nel db.
     */
    public function getAllDisco(){

        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT name, piva FROM disco";
            $disco=$db->fetchAll($statement);
            if (!$disco) {
            throw new Exception("Could not find row");
              }
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $disco;
    }

    public function getStatement($statement){
        
        try{ 
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $discoList=$db->fetchAll($statement);
            if (!$discoList){
                $disco = null;
            } else {
                $disco = $discoList[0];
            }
            // i close the connection
            $db->closeConnection();

        }
        catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        
        return $disco;
    }
    
    
    // if type=1 make a query based on titolo and piva else based on id
    public function getEventDisco($title, $piva, $id, $type){
        
            $title=addslashes($title); // i add the / in case i have the ' in the title
        
            try {
                require_once "models/Db/Db_Db.php";
                
                $db = Db_Db::conn();
                if ($type==1)
                    $statement = "SELECT * FROM edisco WHERE titolo='" . $title . "' AND piva='" . $piva . "'";
                else
                    $statement= "SELECT * FROM edisco WHERE id='" . $id . "'";
                
                $data = $db->fetchAll($statement);
                
                $data = $data[0];

                // i close the connection
                $db->closeConnection();
            } catch (Zend_Db_Exception $e) {
                echo $e->getMessage();
            }
            
            return $data;
    }

    /*
     * get every active event for the disco.
     *
     */
    public function getEventDiscoByPiva($piva){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();

            //get all events (active or not)
            $statement="SELECT * FROM edisco WHERE piva='" . $piva . "'";
            $event=$db->fetchAll($statement);

            $db->closeConnection();

            //var_dump($event);
        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $event;
    }



    /*
     * get every active event for one disco.
     *
     */
    public function getActiveEventDiscoByPiva($piva){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            //get only active events
            $status=1;
            $statement="SELECT * FROM edisco WHERE piva='"
                        . $piva . "' AND status='" . $status . "'";
            $event=$db->fetchAll($statement);

            $db->closeConnection();
            //var_dump($event);
        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        return $event;
    }

    
    public function addEventDisco($disco){
        
            try {
                require_once "models/Db/Db_Db.php";

                $db = Db_Db::conn();
                $db->insert('edisco', $disco);

                // i close the connection
                $db->closeConnection();
            } catch (Zend_Db_Exception $e) {
                echo $e->getMessage();
            }
    }
    
    public function updateEventDisco($table,$newValue,$condition){
        
         try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $res=$db->update($table,$newValue,$condition);
            //$this->view->discoList=$discoList;

            // i close the connection
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
    }
    
    public function showAllDiscoEvent(){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT *
                FROM edisco";
            $discoList=$db->fetchAll($statement);

            //$this->view->discoList=$discoList;

            // i close the connection
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
        
        return $discoList;
    }
    
    public function showAllDisco(){

        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT id,titolo,descrizione,status
                FROM edisco";
            $discoList=$db->fetchAll($statement);

            // i close the connection
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }

        //$this->view->disco = $discoList;
        return $discoList;
    }

    public function showAllDiscoByPiva($piva){
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT id,titolo,descrizione,status
                FROM edisco WHERE piva='" .$piva. "'";
            $discoList=$db->fetchAll($statement);

            // i close the connection
            $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }

        //$this->view->disco = $discoList;
        return $discoList;
    }

    public function saveClub($table, $data, $condition){
                // the condition for the update query
        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $res=$db->update($table,$data,$condition);
            
            // i close the connection
            $db->closeConnection();
            $success = "Modifica avvenuta con successo.";

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
            $success = $e;
        }
    
        return $success;
    }

    /**
     * this method is used to see if there is already an image with that piva
     * @param <type> $piva the piva to check if exist
     * @return <type> 0 if the record doesn't exist else return 1
     */
    public function existDiscoImage($piva){
         try {

            require_once "models/Db/Db_Db.php";

            $db = Db_Db::conn();

            $statement="SELECT piva FROM immagine_disco WHERE piva='" . $piva . "'";

            $disco=$db->fetchAll($statement);
            // i close the connection
            $db->closeConnection();
        } catch (Zend_Db_Exception $e) {
            echo $e->getMessage();
            
        }

        //print_r($disco);
            if(empty($disco)) // i dont have that recort
                return 0;
            else
                return 1;
    }

    /**
     * this method save the data into the db for disco images or event images
     *
     * @param $image the data to save in the db
     * @param $discoOrNot discoOrno==1 disco image else event image
     * @param $exist is 1 if the image aready exist
     *
     */
    public function setImageData($image, $discoOrNo, $exist){

        if($discoOrNo==0){
            $table='immagine_evento';
            $condition="id_event='" . $image['id_event'] . "'";
        }
        else{
            $table='immagine_disco';
            $condition = "piva='" . $image['piva'] . "'";
        }
        //echo $condition;

        try {

            require_once "models/Db/Db_Db.php";

            $db = Db_Db::conn();

            if ($exist == 1)
                $db->update($table, $image, $condition);
            else
                $db->insert($table, $image);
                
            // i close the connection
            $db->closeConnection();
        } catch (Zend_Db_Exception $e) {
            echo $e->getMessage();
        }


    }

}

